package org.flint92.springboot.jpa.controller;

import lombok.extern.slf4j.Slf4j;
import org.flint92.springboot.jpa.entity.User;
import org.flint92.springboot.jpa.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author flint92
 */
@RestController
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/user/{id}")
    public User findUserById(@PathVariable int id) {
        return userService.findById(id);
    }

    @PostMapping("/user")
    public String saveUser(@RequestBody User user) {
        try {
            userService.saveUser(user);
            return "success";
        } catch (Exception e) {
            log.error(e.getLocalizedMessage(), e);
            return e.getLocalizedMessage();
        }
    }

}
